#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
    int findLongestChain(vector<vector<int>>& pairs) {
        if (pairs.size() < 2) return pairs.size();
        sort(pairs.begin(), pairs.end(), [](const vector<int>& left, const vector<int>& right) {return left[0] < right[0]; });

        vector<int> dp(pairs.size(), 1);
        for (int i = 0; i < pairs.size(); ++i) {
            int j = i - 1;
            while (j >= 0 && pairs[i][0] <= pairs[j][1])--j;
            if (j >= 0)
                dp[i] = dp[j] + 1;
        }
        return dp.back();
    }
};